﻿Imports System.Runtime.InteropServices
Imports System.IO


Module Module1

    ''' <summary>
    ''' This sample console program will list all the Solid Edge File Properties associated with the supplied file.
    ''' 
    ''' This program accepts one command line argument, the full path to the file to analyse
    ''' 
    ''' Sample Usage: FileProperties_EnumerateAllProperties "c:\Models\Model.asm" 
    ''' 
    ''' This program is based on the File Properties sample API program in the Solid Edge .NET Programmers Guide
    ''' http://www.plm.automation.siemens.com/zh_tw/Images/Solid_Edge_API_tcm943-125829.pdf
    ''' </summary>

    Sub Main()
        Dim objPropertySets As SolidEdgeFileProperties.PropertySets = Nothing
        Dim objProperties As SolidEdgeFileProperties.Properties = Nothing
        Dim objProperty As SolidEdgeFileProperties.Property = Nothing
        Dim i As Integer
        Dim j As Integer
        'Dim strFormat1 As String = "[{0}]"
        'Dim strFormat2 As String = "{0} = {1}"
        Dim strFileName As String = ""
        Try

            'set the file name to the passed argument
            strFileName = System.Environment.GetCommandLineArgs(1)
            If Not File.Exists(strFileName) Then
                Throw New ApplicationException(strFileName & " File does not exist")
            End If

            ' Create new instance of the PropertySets object
            objPropertySets = New SolidEdgeFileProperties.PropertySets
            ' Open a fileFile Properties
            objPropertySets.Open(strFileName, True)


            ' Example: Loop through all properties 
            ' Note that indexes are zero based 
            For i = 0 To objPropertySets.Count - 1
                objProperties = objPropertySets.Item(i)
                Console.WriteLine("[" & objProperties.Name & "]")
                ' Note that indexes are zero based 
                For j = 0 To objProperties.Count - 1
                    objProperty = objProperties.Item(j)
                    ' .Value property may throw an exception 
                    Try
                        Console.WriteLine("  " & objProperty.Name & " = " & objProperty.Value)
                    Catch ex As Exception
                        Console.WriteLine("  " & objProperty.Name & " = (ERROR)")
                    End Try
                Next
                Console.WriteLine()
            Next

            ' Get a reference to the SummaryInformation properties 
            objProperties = objPropertySets.Item("SummaryInformation")
            ' Get a reference to the Title property by name
            objProperty = objProperties.Item("Title")


        Catch ex As Exception
            Console.WriteLine(ex.Message)
        Finally
            If Not (objProperty Is Nothing) Then
                Marshal.ReleaseComObject(objProperty)
                objProperty = Nothing
            End If
            If Not (objProperties Is Nothing) Then
                Marshal.ReleaseComObject(objProperties)
                objProperties = Nothing
            End If
            If Not (objPropertySets Is Nothing) Then
                ' Close underlying property storage 
                objPropertySets.Close()
                Marshal.ReleaseComObject(objPropertySets)
                objPropertySets = Nothing
            End If
        End Try

        Console.WriteLine("Press any key to close.")
        Console.ReadKey()

    End Sub
End Module
